package gui;


import java.awt.EventQueue;
import java.awt.TextArea;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;

public class InstDataFrame {

	private JFrame frame;
	private JComboBox rA;
	private JComboBox rB;
	private JComboBox rC;
	private JTextField memAddress;
	private JTextField value;
	private JTextField address;
	private JTextField immC;
	private JTextField immB;	
	private JTextField r0;
	private JTextField r1;
	private JTextField r2;
	private JTextField r3;
	private JTextField r4;
	private JTextField r5;
	private JTextField r6;
	private JTextField r7;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InstDataFrame window = new InstDataFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public InstDataFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 827, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel instPanel = new JPanel();
		instPanel.setBounds(10, 11, 791, 440);
		frame.getContentPane().add(instPanel);
		instPanel.setLayout(null);
		
		JLabel lblAssemblyProgram = new JLabel("Assembly Program:");
		lblAssemblyProgram.setFont(new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 16));
		lblAssemblyProgram.setBounds(10, 11, 224, 33);
		instPanel.add(lblAssemblyProgram);
		
		JLabel lblMemoryStartAddress = new JLabel("Memory start address:");
		lblMemoryStartAddress.setBounds(34, 55, 133, 14);
		instPanel.add(lblMemoryStartAddress);
		
		memAddress = new JTextField();
		memAddress.setBounds(231, 52, 44, 20);
		instPanel.add(memAddress);
		memAddress.setColumns(10);
		
		final JLabel memAddReq = new JLabel("required");
		memAddReq.setForeground(Color.RED);
		memAddReq.setVisible(false);
		memAddReq.setFont(new Font("Tahoma", Font.ITALIC, 11));
		memAddReq.setBounds(342, 55, 46, 14);
		instPanel.add(memAddReq);
		
		JLabel lblLoadDataIn = new JLabel("Load data in memory");
		lblLoadDataIn.setBounds(610, 83, 152, 14);
		instPanel.add(lblLoadDataIn);
		
		value = new JTextField();
		value.setBounds(682, 108, 44, 20);
		instPanel.add(value);
		value.setColumns(10);
		
		JLabel lblValue = new JLabel("Value:");
		lblValue.setBounds(596, 111, 46, 14);
		instPanel.add(lblValue);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(596, 140, 65, 14);
		instPanel.add(lblAddress);
		
		address = new JTextField();
		address.setBounds(682, 137, 44, 20);
		instPanel.add(address);
		address.setColumns(10);				
		
		final TextArea memDataLog = new TextArea();
		memDataLog.setEditable(false);
		memDataLog.setBounds(560, 225, 205, 158);
		instPanel.add(memDataLog);
		
		// memory load button
		JButton btnNewButton = new JButton("ADD");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Initialise the memory before then use memory dot store
				String s = "Added " + value.getText() + " to memory address " + address.getText() + "\n";
				memDataLog.setText(memDataLog.getText() + s);
				value.setText("");
				address.setText("");
			}
		});
		btnNewButton.setBounds(629, 172, 71, 23);
		instPanel.add(btnNewButton);
		
		JLabel lblInstructions = new JLabel("Instruction");
		lblInstructions.setBounds(34, 111, 76, 14);
		instPanel.add(lblInstructions);
		
		final TextArea instLog = new TextArea();		
		instLog.setEditable(false);
		instLog.setBounds(34, 225, 258, 158);
		instPanel.add(instLog);
		
		JButton btnFinish = new JButton("Finish");
		btnFinish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean valid = true;
				if (memAddress.getText().equals("")) {
					memAddReq.setVisible(true);
					valid = false;
				} else {
					memAddReq.setVisible(false);
				}
				if (valid) {
					// work on the simulation
					// sawna go to left
				}			
			}
		});
		btnFinish.setBounds(650, 400, 89, 23);
		instPanel.add(btnFinish);
		
		JButton button = new JButton("<- Back");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// go back to archi
			}
		});
		button.setBounds(30, 400, 89, 23);
		instPanel.add(button);				
		
		JLabel lblRega = new JLabel("RegA");
		lblRega.setBounds(148, 111, 65, 14);
		instPanel.add(lblRega);
		
		rA = new JComboBox();
		rA.setModel(new DefaultComboBoxModel(new String[] {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		rA.setBounds(148, 137, 45, 20);
		instPanel.add(rA);
		
		final JLabel lblRegb = new JLabel("RegB");
		lblRegb.setBounds(229, 111, 46, 14);
		instPanel.add(lblRegb);
		
		rB = new JComboBox();
		rB.setModel(new DefaultComboBoxModel(new String[] {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		rB.setBounds(230, 137, 45, 20);
		instPanel.add(rB);
		
		final JLabel lblRegc = new JLabel("RegC");
		lblRegc.setBounds(305, 111, 46, 14);
		instPanel.add(lblRegc);
		
		rC = new JComboBox();
		rC.setModel(new DefaultComboBoxModel(new String[] {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		rC.setBounds(306, 137, 45, 20);
		instPanel.add(rC);
		
		immB = new JTextField();
		immB.setBounds(231, 168, 44, 20);
		immB.setVisible(false);
		instPanel.add(immB);
		immB.setColumns(10);
		
		immC = new JTextField();
		immC.setBounds(302, 168, 44, 20);
		immC.setVisible(false);
		instPanel.add(immC);
		immC.setColumns(10);
		
		final JComboBox instruction = new JComboBox();
		instruction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			if (instruction.getSelectedIndex() == 0 || instruction.getSelectedIndex() == 2 || instruction.getSelectedIndex() == 3
						|| instruction.getSelectedIndex() == 4) {					
				lblRegc.setText("RegC");
				lblRegc.setVisible(true);
				lblRegb.setVisible(true);
				immC.setVisible(false);
				immB.setVisible(false);
				rC.setVisible(true);
				rB.setVisible(true);
			} else if(instruction.getSelectedIndex() == 1 || instruction.getSelectedIndex() == 5 || instruction.getSelectedIndex() == 6
					|| instruction.getSelectedIndex() == 7) {
				lblRegc.setText("imm");
				lblRegc.setVisible(true);
				lblRegb.setVisible(true);
				immC.setVisible(true);
				immB.setVisible(false);
				rC.setVisible(false);
				rB.setVisible(true);
			} else if(instruction.getSelectedIndex() == 8) {				
				lblRegc.setVisible(false);
				lblRegb.setText("imm");
				lblRegb.setVisible(true);
				immC.setVisible(false);
				immB.setVisible(true);
				rC.setVisible(false);
				rB.setVisible(false);
			} else if(instruction.getSelectedIndex() == 9) {
				lblRegc.setVisible(false);
				lblRegb.setText("RegB");
				lblRegb.setVisible(true);
				immC.setVisible(false);
				immB.setVisible(false);
				rC.setVisible(false);
				rB.setVisible(true);
			} else if(instruction.getSelectedIndex() == 10) {
				lblRegc.setVisible(false);				
				lblRegb.setVisible(false);	
				immC.setVisible(false);
				immB.setVisible(false);
				rC.setVisible(false);
				rB.setVisible(false);
			}
			}
		});
		instruction.setModel(new DefaultComboBoxModel(new String[] {"ADD", "ADDI", "NAND", "MUL", "DIV", "LW", "SW", "BEQ", "JMP", "JALR", "RET"}));
		instruction.setBounds(32, 137, 78, 20);
		instPanel.add(instruction);
		
		JButton addInst = new JButton("ADD");
		addInst.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// validate and add the instruction to the linkedList
				if (instruction.getSelectedIndex() == 0 || instruction.getSelectedIndex() == 2 || instruction.getSelectedIndex() == 3
						|| instruction.getSelectedIndex() == 4) { // ADD NAND MUL DIV			
					String s = instruction.getSelectedItem() + " " + rA.getSelectedItem() +  " " 
						+ rB.getSelectedItem() + " " + rC.getSelectedItem() + "\n";
					instLog.setText(instLog.getText() + s);
					frame.validate();
					frame.repaint();
			} else if(instruction.getSelectedIndex() == 1 || instruction.getSelectedIndex() == 5 || instruction.getSelectedIndex() == 6
					|| instruction.getSelectedIndex() == 7) { // ADDI LW SW BEQ
				String s = instruction.getSelectedItem() + " " + rA.getSelectedItem() +  " " 
					+ rB.getSelectedItem() + " #" + immC.getText() + "\n";
				instLog.setText(instLog.getText() + s);
				frame.validate();
				frame.repaint();				
			} else if(instruction.getSelectedIndex() == 8) { // JMP			
				String s = instruction.getSelectedItem() + " " + rA.getSelectedItem() +  " #" 
					+ immB.getText() + "\n";
				instLog.setText(instLog.getText() + s);
				frame.validate();
				frame.repaint();				
			} else if(instruction.getSelectedIndex() == 9) { // JALR
				String s = instruction.getSelectedItem() + " " + rA.getSelectedItem() +  " #" 
					+ immB.getText() + "\n";
				instLog.setText(instLog.getText() + s);
				frame.validate();
				frame.repaint();				
			} else if(instruction.getSelectedIndex() == 10) { // RET
				String s = instruction.getSelectedItem() + " " + rA.getSelectedItem() + "\n";
				instLog.setText(instLog.getText() + s);
				frame.validate();
				frame.repaint();
			}			
			}
		});
		addInst.setBounds(117, 183, 89, 23);
		instPanel.add(addInst);
		
		JLabel lblRegisterData = new JLabel("Register data");
		lblRegisterData.setBounds(390, 111, 89, 14);
		instPanel.add(lblRegisterData);
		
		JLabel lblR = new JLabel("R0:");
		lblR.setBounds(390, 140, 46, 14);
		instPanel.add(lblR);
		
		JLabel lblR_1 = new JLabel("R1:");
		lblR_1.setBounds(390, 171, 46, 14);
		instPanel.add(lblR_1);
		
		JLabel lblR_2 = new JLabel("R2:");
		lblR_2.setBounds(390, 203, 46, 14);
		instPanel.add(lblR_2);
		
		JLabel lblR_3 = new JLabel("R3:");
		lblR_3.setBounds(390, 234, 46, 14);
		instPanel.add(lblR_3);
		
		JLabel lblR_4 = new JLabel("R4:");
		lblR_4.setBounds(390, 259, 46, 14);
		instPanel.add(lblR_4);
		
		JLabel lblR_5 = new JLabel("R5:");
		lblR_5.setBounds(390, 284, 46, 14);
		instPanel.add(lblR_5);
		
		JLabel lblR_6 = new JLabel("R6:");
		lblR_6.setBounds(390, 310, 46, 14);
		instPanel.add(lblR_6);
		
		JLabel lblR_7 = new JLabel("R7:");
		lblR_7.setBounds(390, 335, 46, 14);
		instPanel.add(lblR_7);
		
		r0 = new JTextField();
		r0.setBounds(425, 137, 46, 20);
		instPanel.add(r0);
		r0.setColumns(10);
		
		r1 = new JTextField();
		r1.setText("");
		r1.setBounds(425, 173, 46, 20);
		instPanel.add(r1);
		r1.setColumns(10);
		
		r2 = new JTextField();
		r2.setBounds(425, 200, 46, 20);
		instPanel.add(r2);
		r2.setColumns(10);
		
		r3 = new JTextField();
		r3.setBounds(425, 231, 46, 20);
		instPanel.add(r3);
		r3.setColumns(10);
		
		r4 = new JTextField();
		r4.setText("");
		r4.setBounds(425, 259, 46, 20);
		instPanel.add(r4);
		r4.setColumns(10);
		
		r5 = new JTextField();
		r5.setBounds(425, 284, 46, 20);
		instPanel.add(r5);
		r5.setColumns(10);
		
		r6 = new JTextField();
		r6.setText("");
		r6.setBounds(425, 309, 46, 20);
		instPanel.add(r6);
		r6.setColumns(10);
		
		r7 = new JTextField();
		r7.setText("");
		r7.setBounds(425, 335, 46, 20);
		instPanel.add(r7);
		r7.setColumns(10);
			
		
	}
}
